﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KPIBalanceScoreCard
{
    public partial class AreaMgrDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            load();
        }

        private void load()
        {
            Session[ApplicationConstants.ITEM_GROUPS.ToString()] = new HierarchyDataset.ItemGroupsDataTable();

            if (rdbRole.SelectedValue != null)
            {
                HierarchyDataset.ItemGroupsDataTable itemGroupDt = new HierarchyDataset.ItemGroupsDataTable();
                itemGroupDt = new HierarchyDatasetTableAdapters.ItemGroupsTableAdapter().GetItemGroupByRole(rdbRole.SelectedValue, Double.Parse(Session[ApplicationConstants.CHOOSEN_YEAR.ToString()].ToString()), Double.Parse(Session[ApplicationConstants.CHOOSEN_MONTH.ToString()].ToString()));

                Session[ApplicationConstants.ITEM_GROUPS.ToString()] = itemGroupDt;
            }
            if (!"".Equals(itemGroupName.Value) && yearDropDown.Text != null && !"".Equals(yearDropDown.Text) )
            {
                Session[ApplicationConstants.SELECTED_ITEM_GROUP.ToString()] = itemGroupName.Value;

                HierarchyDataset.ItemDetailsDataTable detailsDtMTD = new HierarchyDataset.ItemDetailsDataTable();
                HierarchyDataset.ItemDetailsDataTable detailsDtYTD = new HierarchyDataset.ItemDetailsDataTable();

                String orgParent = "";

                try
                {
                    orgParent = new HierarchyDatasetTableAdapters.AreaNBranchTableAdapter().GetAreaNBranch(Session[ApplicationConstants.LOGIN_PK.ToString()].ToString(), Double.Parse(yearDropDown.Text), Double.Parse(monthDropDown.Text)).Rows[0][0].ToString().Trim();
                }
                catch 
                {}

                if (rdbRole.SelectedValue != null && Roles.BM.ToString().Equals(rdbRole.SelectedValue))
                {
                    detailsDtMTD = new HierarchyDatasetTableAdapters.ItemDetailsTableAdapter().GetItemDetailsForBMForParentOrg(ApplicationConstants.MTD.ToString(), itemGroupName.Value, Double.Parse(yearDropDown.Text), Double.Parse(monthDropDown.Text), orgParent);
                    detailsDtYTD = new HierarchyDatasetTableAdapters.ItemDetailsTableAdapter().GetItemDetailsForBMForParentOrg(ApplicationConstants.YTD.ToString(), itemGroupName.Value, Double.Parse(yearDropDown.Text), Double.Parse(monthDropDown.Text), orgParent);

                }
                else // if (rdbRole.SelectedValue != null && !Roles.BM.ToString().Equals(rdbRole.SelectedValue))
                {
                    detailsDtMTD = new HierarchyDatasetTableAdapters.ItemDetailsTableAdapter().GetItemDetailsByRoleForParentOrg(rdbRole.SelectedValue.ToString(), ApplicationConstants.MTD.ToString(), itemGroupName.Value, Double.Parse(yearDropDown.Text), Double.Parse(monthDropDown.Text), orgParent);
                    detailsDtYTD = new HierarchyDatasetTableAdapters.ItemDetailsTableAdapter().GetItemDetailsByRoleForParentOrg(rdbRole.SelectedValue.ToString(), ApplicationConstants.YTD.ToString(), itemGroupName.Value, Double.Parse(yearDropDown.Text), Double.Parse(monthDropDown.Text), orgParent);
                }

                Session[ApplicationConstants.ITEM_DETAILS_MTD.ToString()] = detailsDtMTD;
                Session[ApplicationConstants.ITEM_DETAILS_YTD.ToString()] = detailsDtYTD;
                
            }
            
        }

        protected void yearDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session[ApplicationConstants.CHOOSEN_YEAR.ToString()] = yearDropDown.SelectedValue;
            itemGroupName.Value = "1.Business Target";
            load();
        }

        protected void monthDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session[ApplicationConstants.CHOOSEN_MONTH.ToString()] = monthDropDown.SelectedValue;
            itemGroupName.Value = "1.Business Target";
            load();
        }

        protected void rdbRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            itemGroupName.Value = "1.Business Target";
        }
    }
}